<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/5/18
 * Time: 15:10
 */

namespace app\admin\controller;


use app\model\CommentModel;
use think\facade\Request;
use think\facade\Validate;
use think\facade\View;

class Comment
{
    public function index()
    {
        $status = Request::param("status", "");
        //分页
//        parse_str(Request::query(),$queryParams);
//        $commentList = CommentModel::paginate([
//            'list_rows'=>2,
//            'query'=>$queryParams
//        ]);
        //查找评论数据
        if($status == ""){
            $commentList = CommentModel::select();
        }else{
            $commentList = CommentModel::where('status','=',$status)->select();
        }
        View::assign('commentList',$commentList);
        View::assign('status',$status);
        return View::fetch();
    }
    //表示把评论标记为垃圾评论，移入回收站
    public function garbage()
    {
        //获取评论id
        //修改评论状态status = 2就可以了
        $commentId = Request::param("comment_id");
        $validate = Validate::rule([
            'comment_id|评论id' => 'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check(['comment_id' => $commentId])) {
            echo $validate->getError();
            exit();
        }
        $comment = CommentModel::find($commentId);
        $comment['status']= 2;
        $result = $comment->save();
        return View::fetch('public/tips', ['result' => $result]);

    }
    //评论恢复
    public function resume()
    {
        //获取评论id
        //修改评论状态status = 1就可以了
        $commentId = Request::param("comment_id");
        $validate = Validate::rule([
            'comment_id|评论id' => 'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check(['comment_id' => $commentId])) {
            echo $validate->getError();
            exit();
        }
        $comment = CommentModel::find($commentId);
        $comment['status']= 1;
        $result = $comment->save();
        return View::fetch('public/tips', ['result' => $result,'url'=>'/admin/Comment/index/status/2/']);
    }
    //评论删除
    public function delete()
    {
        $commentId = Request::param("comment_id");
        $validate = Validate::rule([
            'comment_id|评论id' => 'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check(['comment_id' => $commentId])) {
            echo $validate->getError();
            exit();
        }

        $result = CommentModel::destroy($commentId);
        //评论回复和评论删除功能跳转url处理
        return View::fetch('public/tips', ['result' => $result,'url'=>'/admin/Comment/index/status/2/']);
    }

}